// 云函数入口文件
const cloud = require('wx-server-sdk')
const axios = require('axios')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const {
    keywords,
    page,
    pagesize
  } = event


  const config = {
    method: 'get',
    url: encodeURI(`http://pd.musicapp.migu.cn/MIGUM3.0/v1.0/content/search_all.do?text=${keywords}&pageNo=${page}&pageSize=${pagesize}&searchSwitch={"song":1,"album":0,"singer":0,"tagSong":0,"mvSong":0,"songlist":0,"bestShow":0}`),
  }

  const data = await axios(config)
    .then(function (response) {
      return response.data
    })
    .catch(function (error) {
      return error
    })

  return {
    data,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}